package windows
{
import com.bit101.components.Window;
import com.bit101.components.Text;
import com.bit101.components.PushButton;
import flash.events.Event;
import baseitems.Base;
import property.Align;
import property.ItemType;
import flash.geom.Point

	/**
	 * Окно с генерируемым кодом
	 * @author yaempechenki@gmail.com
	 */
	public class CodeWindow extends Window
	{
		private var myText:Text;
		private const endLine:String = "\n";
		public function CodeWindow()
		{
			title = "Code";
			hasMinimizeButton = true;
			width = 420;
			height = 200;
			
			myText = new Text(this, 10, 10, "add component to stage...");
			myText.width = width - 20;
			myText.textField.wordWrap = false;
			myText.height = height - 40;
			
			minimized = true;
		}
		
		public function refresh():void {
			var items:Vector.<Base> = Controller.instance.getItems();
			var result:String = "";
			// в начале покажем инклюды
			var includeMap:Array = [];
			result += "/*" + endLine;
			result += "#pragma once" + endLine;
			result += "#include <cocos2d.h>" + endLine;
			result += "USING_NS_CC" + endLine;
			result += "#include \"ui/GuiConstructor.h\"" + endLine;
			result += "#include \"gui/GuiFactory.h\"" + endLine ;
			result += "#include \"gui/overridden/Menu.h\"" + endLine ;
			for each (var oneitem:Base in items) {
				if (includeMap.indexOf(oneitem.type) != -1) continue; else includeMap.push(oneitem.type);
				var includeText:String = getImportText(oneitem);
				if (includeText.length > 0) {
					result += "#include " + getImportText(oneitem) + endLine;	
				}
			}
			result += "*/" + endLine;
			result += endLine;
			result += "/** created with gui constructor */" + endLine;
			result += "const float spacing = grid2px(1);" + endLine ;
			
			// середина
			var levelList:Object = Controller.instance.getLevelsList();
			for each (var list:Object in levelList) {
				for (var i:int = 0; i < list.length; i++) {
					var item:Base = Controller.instance.getItemById(list[i]);
					//for each (var item:Base in items) {
					result += getConstructorText(item) + endLine;
					if (item.selfParent && item.selfParent.type == ItemType.LAYER) {
						result += getNameText(item.selfParent) + "->addSpacing(0); // additional spacing" + endLine;
					}else {
						result += getNameText(item) + "->" + getAnchorText(item) + endLine;
						result += getNameText(item) + "->" + getPositionText(item) + endLine;
						if (item.type == ItemType.LAYER) {
							result += getNameText(item) + "->setSpace(spacing);" + endLine;
						}
						if (item.type == ItemType.BUTTON) {
							result += getMenuTextForButton(item) + endLine;
						}
						if (item.percentWidthEnabled || item.percentHeightEnabled) {
							if(item.type == ItemType.SPRITE) {
								result += getNameText(item) + "->" + getScaleText(item) + endLine;
							} else {
								result += getNameText(item) + "->" + getContentSizeText(item) + endLine;
							}
						}
					}
					if (item.type != ItemType.BUTTON) {
						result += getParentText(item) + "addChild(" + getNameText(item) + ");" + endLine;
					}
					result += endLine;
				}
			}
			// конец
			for each (var oneitem:Base in items) {
				if (oneitem.type == ItemType.LAYER) {
					result += getNameText(oneitem) + "->alignChildren();"+ endLine;
				}
			}
			myText.text = result;
		}
		
		private function getConstructorText(item:Base):String {
			if (item.type == ItemType.NODE) return "CCNode * " + getNameText(item) + " = CCNode::create();"
			if (item.type == ItemType.LAYER) return item.wstack ? "LayoutHorizontal * " + getNameText(item) + " = LayoutHorizontal::create();" : "LayoutVertical * " + getNameText(item) + " = LayoutVertical::create();";
			if (item.type == ItemType.SPRITE) return "CCSprite * " + getNameText(item) + " = CCSprite::create(\"picture.jpg\");"
			if (item.type == ItemType.LABEL) return "TextLabel * " + getNameText(item) + " = TextLabel::create(Localization::tr(\"need lid\"), scaled(" + item.textFieldSize * 3 + "), BTColor::WHITE, CCSize::ZERO, LabelTextStyle::NORMAL, BTColor::BLACK);"
			if (item.type == ItemType.BUTTON) return "BTButton * " + getNameText(item) + " = BTButton::create(BTButtonView::SETTINGS_GREEN, CCSize(scaled(400), scaled(200)), Localization::tr(\"need button lid\"), scaled(64), this, menu_selector(HereClass::onClick));"
			return "new Some();";
		}
		
		private function getNameText(item:Base):String {
			return item.name;
		}
		
		private function getAnchorText(item:Base):String {
			return "setAnchorPoint(CCPoint::" + item.anchor + ");";
		}
		
		private function getParentText(item:Base):String {
			if (item.selfParent) {
				return getNameText(item.selfParent) + "->";
			}
			return "";
		}
		
		private function getPositionText(item:Base):String {
			var parent:String = getParentText(item);
			var resultText:String = "0, 0"
			if (item.posAlign == Align.TOP_LEFT) resultText = "0, " + parent + "getContentSize().height";
			if (item.posAlign == Align.MIDDLE_LEFT) resultText = "0, " + parent + "getContentSize().height/2";
			if (item.posAlign == Align.BOTTOM_LEFT) resultText = "0, 0"
			
			if (item.posAlign == Align.MIDDLE_TOP) resultText = parent + "getContentSize().width/2, " + parent + "getContentSize().height";
			if (item.posAlign == Align.MIDDLE) resultText = parent + "getContentSize().width/2, " + parent + "getContentSize().height/2";
			if (item.posAlign == Align.MIDDLE_BOTTOM) resultText = parent + "getContentSize().width/2, 0";
			
			if (item.posAlign == Align.TOP_RIGHT) resultText = parent + "getContentSize().width, " + parent + "getContentSize().height";
			if (item.posAlign == Align.MIDDLE_RIGHT) resultText = parent + "getContentSize().width, " + parent + "getContentSize().height/2";
			if (item.posAlign == Align.BOTTOM_RIGHT) resultText = parent + "getContentSize().width, 0";
			
			return "setPosition(CCPoint("+ resultText + "));";
		}
		
		private function getContentSizeText(item:Base):String {
			var parent:String = getParentText(item);
			var haveParent:Boolean = (item.selfParent != null);
			var widthText:String = parent + "getContentSize().width";
			var heigthText:String = parent + "getContentSize().height";
			
			if (item.percentWidthEnabled) {
				widthText = parent + "getContentSize().width" + getPercentValue(item);
			}
			if (item.percentHeightEnabled) {
				heigthText = parent + "getContentSize().height" + getPercentValue(item);
			}
			
			return "setContentSize(CCSize(" +widthText + ", " + heigthText + "));";
		}
		
		private function getScaleText(item:Base):String {
			var parent:String = getParentText(item);
			var haveParent:Boolean = (item.selfParent != null);
			var scaleXText:String = "1";
			var scaleYText:String = "1";
			
			if (item.percentWidthEnabled) {
				scaleXText = parent + "getContentSize().width /" + getNameText(item) + "->getContentSize().width" + getPercentValue(item);
			}
			if (item.percentHeightEnabled) {
				scaleYText = parent + "getContentSize().height /" + getNameText(item) + "->getContentSize().height" + getPercentValue(item);
			}
			return "setScale(" + scaleXText + ", " + scaleYText + ");";
		}
		
		private function getImportText(item:Base):String {
			if (item.type == ItemType.LAYER) return item.wstack ? "\"gui/elements/LayoutHorizontal.h\"" : "\"gui/elements/LayoutVertical.h\""
			if (item.type == ItemType.BUTTON) return "\"ui/button/BTButton.h\"";
			return "";
		}
		
		private function getPercentValue(item:Base):String {
			var result:String = "";
			if (item.percentWidthEnabled && item.percentWidth != 100) {
				result = " * " + (item.percentWidth/100).toString() + "f";
			}
			if (item.percentHeightEnabled && item.percentHeight != 100) {
				result = " * " + (item.percentHeight/100).toString() + "f";
			}
			return result;
		}
		
		
		private function getMenuTextForButton(item:Base):String {
			var result:String = " Menu * menu = Menu::create();" + endLine +
				"menu->setTouchPriority(kCCMenuHandlerPriority - 20);" + endLine +
				"menu->setPosition(CCPoint::ZERO);" + endLine +
				"menu->addChild(" + getNameText(item) + ");"
				
			return result;
		}
	}
}